package com.tablefood.action;

import java.util.Map;

import javax.annotation.Resource;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.tablefood.domain.ShopAdmin;
import com.tablefood.service.AdminService;

public class AdminAction extends ActionSupport {

	private String message;
	private ShopAdmin admin;

	@Resource
	private AdminService adminService;

	public String login() throws Exception {

		//获得用户登录信息
		ActionContext actionContext = ActionContext.getContext();
		Map<String, Object> session = actionContext.getSession();

		//System.out.println(session.get("name"));
		//用户已登录
		String name = (String) session.get("name");
		if (name != null) {
			return SUCCESS;
		}
 
		//判断错误
		if (admin.getAdminuser() == null || admin.getAdminuser().equals("")) {
			this.setMessage("用户名不能为空");
			return LOGIN;
		}
		if (admin.getAdminpass() == null || admin.getAdminpass().equals("")) {
			this.setMessage("密码不能为空");
			return LOGIN;
		}

		//检测用户
		if (adminService.loginAdmin(admin)) {
			session.put("name", admin.getAdminuser());
			return SUCCESS;
		} else {
			this.setMessage("用户名或密码错误");
			return LOGIN;
		}

	}
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public ShopAdmin getAdmin() {
		return admin;
	}

	public void setAdmin(ShopAdmin admin) {
		this.admin = admin;
	}

	public AdminService getAdminService() {
		return adminService;
	}

	public void setAdminService(AdminService adminService) {
		this.adminService = adminService;
	}

}
